package com.gzc.just.play.last.war.sceneserver.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 玩家好友实体类
 * 
 * 对应数据库表：player_friends
 * 作为Phase 4.3数据库持久化集成的一部分
 */
@Entity
@Table(name = "player_friends", 
       indexes = {
           @Index(name = "idx_friends_player_id", columnList = "player_id"),
           @Index(name = "idx_friends_friend_id", columnList = "friend_id")
       },
       uniqueConstraints = {
           @UniqueConstraint(name = "uk_player_friend", columnNames = {"player_id", "friend_id"})
       })
@EntityListeners(AuditingEntityListener.class)
public class PlayerFriend {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "player_id", nullable = false, foreignKey = @ForeignKey(name = "fk_friend_player"))
    @JsonIgnore
    private Player player;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "friend_id", nullable = false, foreignKey = @ForeignKey(name = "fk_friend_friend"))
    @JsonIgnore
    private Player friend;
    
    @Column(name = "status", nullable = false)
    private Integer status = 0;
    
    @Column(name = "request_time", nullable = false)
    private LocalDateTime requestTime;
    
    @Column(name = "confirm_time")
    private LocalDateTime confirmTime;
    
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    // 好友状态常量
    public static final int STATUS_PENDING = 0;   // 待确认
    public static final int STATUS_CONFIRMED = 1; // 已确认
    public static final int STATUS_REJECTED = 2;  // 已拒绝
    
    // 构造函数
    public PlayerFriend() {}
    
    public PlayerFriend(Player player, Player friend) {
        this.player = player;
        this.friend = friend;
        this.status = STATUS_PENDING;
        this.requestTime = LocalDateTime.now();
    }
    
    // Getter和Setter方法
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Player getPlayer() {
        return player;
    }
    
    public void setPlayer(Player player) {
        this.player = player;
    }
    
    public Player getFriend() {
        return friend;
    }
    
    public void setFriend(Player friend) {
        this.friend = friend;
    }
    
    public Integer getStatus() {
        return status;
    }
    
    public void setStatus(Integer status) {
        this.status = status;
    }
    
    public LocalDateTime getRequestTime() {
        return requestTime;
    }
    
    public void setRequestTime(LocalDateTime requestTime) {
        this.requestTime = requestTime;
    }
    
    public LocalDateTime getConfirmTime() {
        return confirmTime;
    }
    
    public void setConfirmTime(LocalDateTime confirmTime) {
        this.confirmTime = confirmTime;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    // 业务方法
    public void confirm() {
        this.status = STATUS_CONFIRMED;
        this.confirmTime = LocalDateTime.now();
    }
    
    public void reject() {
        this.status = STATUS_REJECTED;
    }
    
    public boolean isPending() {
        return this.status == STATUS_PENDING;
    }
    
    public boolean isConfirmed() {
        return this.status == STATUS_CONFIRMED;
    }
    
    public boolean isRejected() {
        return this.status == STATUS_REJECTED;
    }
    
    public String getStatusName() {
        switch (status) {
            case STATUS_PENDING:
                return "待确认";
            case STATUS_CONFIRMED:
                return "已确认";
            case STATUS_REJECTED:
                return "已拒绝";
            default:
                return "未知状态";
        }
    }
    
    @Override
    public String toString() {
        return "PlayerFriend{" +
                "id=" + id +
                ", player=" + (player != null ? player.getPlayerId() : null) +
                ", friend=" + (friend != null ? friend.getPlayerId() : null) +
                ", status=" + status +
                ", statusName='" + getStatusName() + '\'' +
                ", requestTime=" + requestTime +
                ", confirmTime=" + confirmTime +
                ", createdAt=" + createdAt +
                '}';
    }
}